package com.kun.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

//队列过期时间配置
@Configuration
public class TtlConfig {

    //设置交换机
    @Bean
    public DirectExchange ttlExchange(){
        return new DirectExchange("ttl_direct_exchange");
    }

    //设置队列，对队列设置过期时间
    @Bean
    public Queue ttl_queue_direct(){

        Map<String, Object> map = new HashMap<>();

        map.put("x-message-ttl", 5000); //设置队列过期时间为6000ms
        return new Queue("ttl_queue",true,false,false,map);
    }

    //设置队列，不设置过期时间，用于测试过期的第二种方式，对消息设置过期时间
    @Bean
    public Queue ttl_message_queue_direct(){
        return new Queue("ttl_message_queue",true,false,false,null);
    }


    //建立绑定关系
    @Bean
    public Binding ttl_message_binding_direct(){
        return BindingBuilder.bind(ttl_message_queue_direct()).to(ttlExchange()).with("ttl_message_queue_direct");
    }

    //建立绑定关系
    @Bean
    public Binding ttl_binding_direct(){
        return BindingBuilder.bind(ttl_queue_direct()).to(ttlExchange()).with("ttl_queue_direct");
    }

}
